package com.hzit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzit.entity.LoginUser;
import com.hzit.entity.User;
import com.hzit.mapper.UserMapper;
import com.hzit.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public LoginUser login(User user) {
        User lUser = this.getOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        if (!Objects.isNull(lUser)){
            if (StrUtil.isNotBlank(lUser.getPassword())){
                if (user.getPassword().equals(lUser.getPassword())){
                    LoginUser loginUser=new LoginUser();
                    loginUser.setUserId(lUser.getId());
                    loginUser.setUser(lUser);
                    loginUser.setPermissions(this.baseMapper.getPermission(user.getUsername()));
                    return loginUser;
                }
            }
        }
        return null;
    }
}
